﻿using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace PSDUIImporter
{
    internal class InputFieldLayerImport : ILayerImport
    {
        private PSDImportCtrl pSDImportCtrl;

        public InputFieldLayerImport(PSDImportCtrl pSDImportCtrl)
        {
            this.pSDImportCtrl = pSDImportCtrl;
        }

        public void DrawLayer(Layer layer, GameObject parent)
        {
            // 加载模板进行添加
            UnityEngine.UI.InputField inputfield = PSDImportUtility.LoadAndInstant<UnityEngine.UI.InputField>(PSDImporterConst.ASSET_PATH_INPUTFIELD, layer.name, parent);
            GameObject gameObject = inputfield.gameObject;

            // 先获取背景大小/文本框需要
            PSDUIImporter.Size size = new PSDUIImporter.Size();
            PSDUIImporter.Position position = new PSDUIImporter.Position();
            foreach (Layer clayer in layer.layers)
            {
                PSImage image = clayer.image;
                if (image == null) 
                    continue;

                if (image.name.Contains("_Background"))
                {
                    size = image.size;
                    position = image.position;
                    break;
                }
            }

            // 这里要修复布局
            foreach (Layer clayer in layer.layers)
            {
                PSImage image = clayer.image;
                if (image == null) 
                    continue;

                if (image.imageType == ImageType.Label)
                {                    
                    if (image.name.Contains("_Text"))
                    {
                        // 添加一些偏移量fix左右顶满了
                        float offset = size.height / 3;
                        size.width -= offset;
                        // 重设大小为背景大小/位置
                        image.size = size;
                        image.position = position;
                        GameObject go = inputfield.textComponent.gameObject;
                        pSDImportCtrl.DrawImage(image, gameObject, go);
                        go.FixRectTransformTemplate(image);

                        continue;
                    }
                    // 只有提示输入的时候/输入框也进行相同属性处理
                    else if (image.name.Contains("_Holder"))
                    {                        
                        // 添加一些偏移量fix左右顶满了
                        float offset = size.height / 3;
                        size.width -= offset;
                        // 重设大小为背景大小/位置
                        image.size = size;
                        image.position = position;
                        // Holder
                        GameObject go = inputfield.placeholder.gameObject;
                        pSDImportCtrl.DrawImage(image, gameObject, go);
                        go.FixRectTransformTemplate(image);

                        // Text                        
                        image.name = image.name.Replace("_Holder", "_Text");
                        go = inputfield.textComponent.gameObject;
                        pSDImportCtrl.DrawImage(image, gameObject, go);
                        go.FixRectTransformTemplate(image);

                        continue;
                    }
                }
                else
                {
                    // 修复背景&大小
                    if (image.name.Contains("_Background"))
                    {
                        if (image.imageSource == ImageSource.Common || image.imageSource == ImageSource.Custom)
                        {
                            string assetPath = PSDImportUtility.baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX;
                            Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite;
                            inputfield.image.sprite = sprite;

                            gameObject.FixRectTransform(image);
                            
                            continue;                 
                        }
                    }
                }

                // 剩余的需要继续创建
                pSDImportCtrl.DrawLayer(clayer, gameObject);
            }
        }
    }
}